package helper.request.logandreg;

import server.dbconnector.DatabaseConnector;
import helper.response.RegResponse;

import java.sql.*;

import base.User;
public class RegisterRequest implements helper.request.RequestBeforeLogin {
	private static final long serialVersionUID = 989215645124171703L;
	private String userName;
	private String realName;
	private String email;
	private String password;

	public RegisterRequest(String userName, String realName, String email,
			String password) {
		this.userName = userName;
		this.realName = realName;
		this.password = password;
		this.email = email;
	}

	@Override
	public void process(DatabaseConnector dc)throws SQLException {
		dc.commit("insert into user(username,password,realname,sex,email,phonenumber,Approved,isAdmin,isPublicContact) values('"+this.userName+"','"+this.password+"','"+this.realName+"',0,'"+this.email+"','',0,0,0)");
	}
	public RegResponse process(DatabaseConnector dc,int empty)throws SQLException{
		ResultSet rs=dc.query("select * from user where username='"+this.userName+"'");
		if(rs.first())return new RegResponse(true,null);
		else process(dc);
		rs.close();
		ResultSet rs2=dc.query("select * from user where username='"+this.userName+"'");
		rs2.first();
		return new RegResponse(false,new User(rs2.getInt(1), rs2.getString(2), rs2
					.getString(3), rs2.getString(4), rs2.getInt(5),
					rs2.getString(6), rs2.getString(7), false));
	}
}
